home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / game / shoot / ADescentSrc.lha / descent / ui / icon.c < prev    next >
C/C++ Source or Header  |  1998-08-08  |  3KB  |  144 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13.  
  14. #include <stdlib.h>
  15. #include <string.h>
  16.  
  17. #include "mem.h"
  18. #include "fix.h"
  19. #include "types.h"
  20. #include "gr.h"
  21. #include "ui.h"
  22. #include "key.h"
  23.  
  24. #define Middle(x) ((2*(x)+1)/4)
  25.  
  26. extern void ui_draw_shad( short x1, short y1, short x2, short y2, short c1, short c2 );
  27.  
  28. void ui_draw_box_in1( short x1, short y1, short x2, short y2 )
  29. {
  30.  
  31.     gr_setcolor( CWHITE );
  32.     gr_urect( x1+1, y1+1, x2-1, y2-1 );
  33.  
  34.     ui_draw_shad( x1+0, y1+0, x2-0, y2-0, CGREY, CBRIGHT );
  35. }
  36.  
  37.  
  38. void ui_draw_icon( UI_GADGET_ICON * icon )
  39. {
  40.     int height, width, avg;
  41.     int x, y;
  42.     
  43.     
  44.     if ((icon->status==1) || (icon->position != icon->oldposition))
  45.     {
  46.         icon->status = 0;
  47.  
  48.         ui_mouse_hide();
  49.     
  50.         gr_set_current_canvas( icon->canvas );
  51.         gr_get_string_size(icon->text, &width, &height, &avg );
  52.     
  53.         x = ((icon->width-1)/2)-((width-1)/2);
  54.         y = ((icon->height-1)/2)-((height-1)/2);
  55.  
  56.         if (icon->position==1 )
  57.         {
  58.             // Draw pressed
  59.             ui_draw_box_in( 0, 0, icon->width, icon->height );
  60.             x += 2; y += 2;
  61.         }
  62.         else if (icon->flag)
  63.         {
  64.             // Draw part out
  65.             ui_draw_box_in1( 0, 0, icon->width, icon->height );
  66.             x += 1; y += 1;    
  67.         }
  68.         else
  69.         {
  70.             // Draw released!
  71.             ui_draw_box_out( 0, 0, icon->width, icon->height );
  72.         }
  73.     
  74.         gr_set_fontcolor( CBLACK, -1 );        
  75.         gr_ustring( x, y, icon->text );
  76.  
  77.         ui_mouse_show();
  78.     }
  79. }
  80.  
  81.  
  82. UI_GADGET_ICON * ui_add_gadget_icon( UI_WINDOW * wnd, char * text, short x, short y, short w, short h, int k,int (*f)(void) )
  83. {
  84.     UI_GADGET_ICON * icon;
  85.  
  86.     icon = (UI_GADGET_ICON *)ui_gadget_add( wnd, 9, x, y, x+w-1, y+h-1 );
  87.  
  88.     icon->width = w;
  89.     icon->height = h;
  90.     //MALLOC( icon->text, char, strlen( text )+2);//Hack by KRB
  91.     icon->text=(char *)malloc((strlen( text )+2)*sizeof(char));
  92.     strcpy( icon->text, text );
  93.     icon->trap_key = k;
  94.     icon->user_function = f;
  95.     icon->oldposition = 0;
  96.     icon->position = 0;
  97.     icon->pressed = 0;
  98.     icon->canvas->cv_font = ui_small_font;
  99.  
  100.     // Call twice to get original;
  101.     if (f)
  102.     {
  103.         icon->flag = (byte)f();
  104.         icon->flag = (byte)f();
  105.     } else {
  106.         icon->flag = 0;
  107.     }
  108.  
  109.  
  110.     return icon;
  111.  
  112. }
  113.  
  114. void ui_icon_do( UI_GADGET_ICON * icon, int keypress )
  115. {
  116.     int OnMe;
  117.  
  118.     OnMe = ui_mouse_on_gadget( (UI_GADGET *)icon );
  119.  
  120.     icon->oldposition = icon->position;
  121.  
  122.     if ( B1_PRESSED && OnMe )
  123.     {
  124.         icon->position = 1;
  125.     } else  {
  126.         icon->position = 0;
  127.     }
  128.  
  129.     icon->pressed = 0;
  130.  
  131.     if ((icon->position==0) && (icon->oldposition==1) && OnMe )
  132.         icon->pressed = 1;
  133.  
  134.     if (icon->pressed == 1 || keypress==icon->trap_key )
  135.     {
  136.         icon->status = 1;
  137.         icon->flag = (byte)icon->user_function();
  138.         if (keypress==icon->trap_key) last_keypress = 0;
  139.     }
  140.  
  141.     ui_draw_icon( icon );
  142.  
  143. }
  144.